package com.violet.themvp

import android.os.Bundle
import android.widget.Toast
import com.violet.constlib.exception.ExceptionHandle
import com.violet.themvp.presenter.ActivityPresenter
import com.violet.themvp.utils.ObjectInstanceManager
import com.violet.themvp.view.IDelegate
import com.violet.themvp.view.IDisplay
import com.violet.themvp.widget.LoadingDialog

/**
 * UI 写在IDelegate 实现类中
 */
abstract class BaseDiAct<V: IDelegate>: ActivityPresenter<V>(), IDisplay {

    /**
     * 让带有@AgainInstance 注解的 field 在app意外杀死重启后可以自动重新实例化
     */
    private var mObjectInstanceManager = ObjectInstanceManager()

    private var mLoadingDialog: LoadingDialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mLoadingDialog = LoadingDialog(this)
        if (savedInstanceState == null) {
            initData()
        } else {
            resetData(savedInstanceState)
        }
    }

    override fun initData() {
        //empty implement
    }

    /**
     * app长时间滞留后天，被系统意外杀死。重启
     * 注意不要在这个方法里面重新实例化fragment，这样会导致相同的fragment有多个
     *
     * @param savedInstanceState
     */
    override fun resetData(savedInstanceState: Bundle) {
        mObjectInstanceManager.againFragmentInstance(supportFragmentManager, this)
        mObjectInstanceManager.againFieldInstance(savedInstanceState, this)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
        mObjectInstanceManager.saveField(outState, this)
    }

    protected open fun showLoading() {
        showLoading("加载中...")
    }

    protected open fun showLoading(text: String?) {
        if (mLoadingDialog != null && !mLoadingDialog!!.isShowing) {
            setLoadingText(text)
            mLoadingDialog!!.show()
        }
    }

    protected open fun hideLoading() {
        if (mLoadingDialog != null && mLoadingDialog!!.isShowing) {
            mLoadingDialog!!.dismiss()
        }
    }

    protected open fun setLoadingText(loadingText: String?) {
        mLoadingDialog!!.setLoadingText(loadingText)
    }

    protected open fun isLoading(): Boolean {
        return mLoadingDialog != null && mLoadingDialog!!.isShowing
    }


    protected open fun toast(msg: CharSequence?) {
        viewDelegate.toast(msg)
    }

    protected open fun toastError(throwable: Throwable?) {
        val responeThrowable: ExceptionHandle.ResponeThrowable = ExceptionHandle.handleException(throwable)
        viewDelegate.toast("${responeThrowable.message}${responeThrowable.getCode()}")
    }

    private var mExitToast: Toast? = null
    private var firstTimeOfClickBackKey: Long = 0

    protected open fun onExit() {
        if (mExitToast == null) {
            mExitToast = Toast.makeText(this, "再按一次退出", Toast.LENGTH_SHORT)
        }
        val time = System.currentTimeMillis()
        val timeDelta = time - firstTimeOfClickBackKey
        if (timeDelta <= 2000L) {
            finish()
        } else {
            firstTimeOfClickBackKey = time
            mExitToast!!.show()
        }
    }
}